package com.boc.langxiang.ui.activity.me;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.boc.langxiang.App;
import com.boc.langxiang.R;
import com.boc.langxiang.contract.me.MyExerciseActivityContract;
import com.boc.langxiang.entity.response.TobecanyuContentBean;
import com.boc.langxiang.presenter.me.MyExerciseActivityPresenter;
import com.boc.langxiang.ui.adapter.me.MyExercisePagerAdapter;
import com.boc.langxiang.ui.adapter.me.MyExerciseWantFragmentRVAdapter;
import com.boc.langxiang.ui.fragment.me.MyExerciseWantFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的活动
 */
public class MyExerciseActivity extends MeBaseToolBarActivity implements MyExerciseActivityContract.View {


    @BindView(R.id.tablayout_me_my_exercise)
    TabLayout tablayoutMeMyExercise;
    @BindView(R.id.vp_me_my_exercise)
    ViewPager vpMeMyExercise;

    private MyExerciseActivityPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_exercise);
        ButterKnife.bind(this);
        addTextMenu(getmToolbar());
        setToolBarTitle("我的活動");
        initViewPresenter();
    }

    private void initViewPresenter() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            tablayoutMeMyExercise.setLayoutMode(TabLayout.MODE_FIXED);
        }
        mPresenter = new MyExerciseActivityPresenter(this);
        mPresenter.initFragmentList();
        mPresenter.initAdapter(getSupportFragmentManager());
    }


    private void addTextMenu(final Toolbar toolbar) {
        View menu = getLayoutInflater().inflate(R.layout.menu_me_myexercise_edit, null);
        final TextView text = (TextView) menu.findViewById(R.id.tv_me_myexercise_edit);
        text.setTextColor(ContextCompat.getColor(App.getInstance(), R.color.colorSecondaryText));
        text.setText("编辑");
        Toolbar.LayoutParams menuParams = new Toolbar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        menuParams.gravity = Gravity.RIGHT;
        toolbar.addView(menu, menuParams);
        menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyExercisePagerAdapter adapter = (MyExercisePagerAdapter) vpMeMyExercise.getAdapter();
                int currentItem = vpMeMyExercise.getCurrentItem();
                if (currentItem == 0) {
                    MyExerciseWantFragment fragment = (MyExerciseWantFragment) adapter.getItem(currentItem);
                    MyExerciseWantFragmentRVAdapter wantAdapter = fragment.getmAdapter();
                    List<TobecanyuContentBean> data = wantAdapter.getData();
                    if (data == null || data.isEmpty()) {
                        text.setText("编辑");
                        return;
                    }
                    if (text.getText().toString().equals("编辑")) {
                        text.setText("完成");
                    } else {
                        text.setText("编辑");
                    }
                    wantAdapter.setDeleteIconVisiblity(!fragment.getIconVisible());
                    fragment.setIconVisible(!fragment.getIconVisible());
                } else if (currentItem == 1) {
                    //参与的想删除
                /*    MyExerciseJoinFragment fragment = (MyExerciseJoinFragment) adapter.getItem(currentItem);
                    MyExerciseJoinFragmentRVAdapter joinAdapter = fragment.getmAdapter();
                    joinAdapter.setDeleteIconVisiblity(!fragment.getIconVisible());
                    fragment.setIconVisible(!fragment.getIconVisible());*/
                }
            }
        });

    }


    @Override
    protected int setToolBarBackgroundColor() {
        return Color.WHITE;
    }

    @Override
    protected int setToolBarBackImg() {
        return R.drawable.icon_me_left;
    }

    @Override
    protected int setToolBarTitleColor() {
        return Color.BLACK;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(String code, String msg) {

    }

    @Override
    public void setAdapter(MyExercisePagerAdapter mAdapter) {
        vpMeMyExercise.setAdapter(mAdapter);
        tablayoutMeMyExercise.setupWithViewPager(vpMeMyExercise);
    }
}
